package net.lwf.xml.parser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


/**
 * General XML document in the KP J2EE framework. 
 * 
 * It encapsulates an org.w3c.dom.Document and offers convinient utility methods.
 * 
 * 
 */


public class DomDocumentImpl 
	implements IDomDocument {
	
	private Document m_doc = null;
	
	public DomDocumentImpl(org.w3c.dom.Document doc) {
		m_doc = doc;
	}

	/**
	 * This method reads in the xml file and returns handle to the
	 * xml document, and object of org.w3cdom.Document.
	 * @updated by
	 */
	public Document getDocument() {
		return m_doc;	
	}


	/**
	* This method returns handle to the
	* root element of the given xml document.
	* @updated by
	*/
	public Element getRoot() {
		Element element = m_doc.getDocumentElement();
		//System.out.println("===> getRoot: Node Name: " + element.getNodeName());
		//System.out.println("===> getRoot: Tag Name Name: " + element.getTagName());
		//System.out.println("===> getRoot: Name space URI: " + element.getNamespaceURI());
		return element;
	}
	
	/**
	 * This method returns all the child elements of a given parent element.
	 * @updated by
	 */
	public List getChildElements(Element parent)
		//throws org.kp.j2ee.xml.XMLDeclerativeException {
	throws Exception {
		List subElements = new ArrayList();
		if (parent == null){
			java.lang.String msg = "Null Parent org.w3c.dom.Element Provided";
			//throw new org.kp.j2ee.xml.XMLDeclerativeException (msg, new java.lang.NullPointerException("Exception in DomDocumentImpl"));
		}
			
		NodeList childrens = parent.getChildNodes();
		for (int i = 0; i < childrens.getLength(); i++) {
			if (Node.ELEMENT_NODE == childrens.item(i).getNodeType()) {
				Element element = (Element) childrens.item(i);
				subElements.add(element);
				//getChildElements(element);
			}
			
		}
		return subElements;
	}
	
	public boolean hasChildren(Element element) {
		NodeList nodelist = element.getChildNodes();
		if(nodelist.getLength() != 0) {
			return true;
		}
		return false;
	}
	

	
	/**
	 * This method returns all the child elements of a given parent element.
	 * @updated by
	 */
	public List getGrandChildren(Element parent)
	throws Exception {
		ArrayList subElements = new ArrayList();
		if (parent == null){
			java.lang.String msg = "Null Parent org.w3c.dom.Element Provided";
		}
			
		NodeList childrens = parent.getChildNodes();
		
		for (int i = 0; i < childrens.getLength(); i++) {
			if (Node.ELEMENT_NODE == childrens.item(i).getNodeType()) {
				Element element = (Element) childrens.item(i);
				subElements.add(element);
				Map attributes = getAttributes(element);
			}
		}
		return subElements;
	}

	public List getChildElementsOfRoot()
		//throws org.kp.j2ee.xml.XMLDeclerativeException {
	throws Exception {

		return this.getChildElements(this.getRoot());
	}

	// returns attrbitues for an element
	public HashMap getAttributes(Element anElement) {
		
		NamedNodeMap attrs = anElement.getAttributes();
		HashMap hashMap = new HashMap();
		
		for (int i=0; i<attrs.getLength(); i++) {
			hashMap.put(attrs.item(i).getNodeName(), attrs.item(i).getNodeValue());
		}

		return hashMap;
	}
	/**
	* This method returns value of the given elements' attribute.
	* @updated by
	*/
	public String getAttributeValue(Element e, String name) {
			
		if (e == null){
			java.lang.String msg = "Null org.w3c.dom.Element Provided";
			//throw new Exception (msg, new NullPointerException("Exception in DomDocumentImpl"));
		}
		return e.getAttribute(name);
	}
}


